package com.taiji.quartz.task;

import com.taiji.common.core.domain.entity.SysUser;
import com.taiji.common.utils.uuid.IdUtils;
import com.taiji.message.domain.OrderWarn;
import com.taiji.message.mapper.OrderWarnMapper;
import com.taiji.system.mapper.SysUserMapper;
import com.taiji.workOrder.Utils.Const;
import com.taiji.workOrder.domain.Message;
import com.taiji.workOrder.domain.MessageBody;
import com.taiji.workOrder.domain.ServerMonitorData;
import com.taiji.workOrder.domain.WorkOrder;
import com.taiji.workOrder.mapper.MessageMapper;
import com.taiji.workOrder.mapper.MyOrderMapper;
import com.taiji.workOrder.mapper.ServerMonitorDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.taiji.common.utils.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    @Autowired
    private MyOrderMapper myOrderMapper;

    @Autowired
    private OrderWarnMapper orderWarnMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 工单延期定时任务
     */
    public void myWorkOrderTask() {
        System.out.println("工单管理,每日定时调度筛选。超期变为“已延期”状态。");
        Date date = new Date();
        //查询延时工单
        List<WorkOrder> workOrders = myOrderMapper.selectMyOrderByOverPlanTime(date);
        if (!workOrders.isEmpty()){
            ArrayList<String> list = new ArrayList<>();

            for (WorkOrder workOrder : workOrders) {
                myOrderMapper.updateMyOrderStatus(workOrder.getId(), Const.WorkOrderStatus.DELAYED.getKey());
                list.add(workOrder.getId());
                //给工单创建人 发送延时消息
                Message message = new Message();
                message.setId(IdUtils.randomUUID());
                message.setFromName("system");
                message.setTitle(workOrder.getWorkNumber()+"超时提醒");
                //message.setContent(workOrder.getName()+"工单延期,工单编号:"+workOrder.getWorkNumber());
                message.setContent(workOrder.getWorkNumber()+"工单超时"+"\n"+"超时发起人：系统");
                message.setSendTime(new Date());
                message.setProjectCode(workOrder.getProjectCode());
                message.setServerName("");
                message.setInfoType("延期");
                message.setStatus(0);
                message.setToName(workOrder.getCreateBy());
                messageMapper.insertMessage(message);
                //当前处理人和创建人不是同一个人，则为当前处理人发送延时消息
                if (!workOrder.getCreateBy().equals(workOrder.getDealPerson())){
                    message.setId(IdUtils.randomUUID());
                    message.setToName(workOrder.getDealPerson());
                    messageMapper.insertMessage(message);
                }
                List<SysUser> sysUsers = sysUserMapper.selectProjectManagerUserName(workOrder.getProjectCode());
                for (SysUser sysUser : sysUsers) {
                    if (sysUser.getUserName().equals(workOrder.getCreateBy())){
                        continue;
                    }
                    if (sysUser.getUserName().equals(workOrder.getDealPerson())){
                        continue;
                    }
                    message.setId(IdUtils.randomUUID());
                    message.setToName(sysUser.getUserName());
                    messageMapper.insertMessage(message);
                }
            }

            List<WorkOrder> workOrders1 = myOrderMapper.selectMyOrderByIdGroupByProjectCode(list);
            for (WorkOrder workOrder : workOrders1) {
                //发送延时定时任务执行消息
                OrderWarn orderWarn = new OrderWarn();
                orderWarn.setProjectCode(workOrder.getProjectCode());
                orderWarn.setId(IdUtils.fastSimpleUUID());
                orderWarn.setTitle("延期判断定时任务执行成功");
                orderWarn.setCreateTime(date);
                orderWarn.setStatus("0");
                orderWarn.setType("定时任务");
                orderWarn.setContents("延期工单编号："+workOrder.getWorkNumber());
                orderWarn.setChangeNumber(Integer.parseInt(workOrder.getId()));
                orderWarnMapper.insertOrderWarn(orderWarn);
            }


        }


    }


    @Autowired
    private ServerMonitorDataMapper serverMonitorDataMapper;
    /**
     * 运维驾驶舱定时调度任务
     */
    public void operationCockpitTask(){

        //查询已过期3天的监控信息
        List<String> ids = serverMonitorDataMapper.selectExpiredServerMonitorData();
        if (!ids.isEmpty()){
            int i = serverMonitorDataMapper.deleteServerMonitorDataByIds(ids.toArray(new String[0]));
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.M.d");
            String time = now.format(formatter);
            OrderWarn orderWarn = new OrderWarn();
            orderWarn.setProjectCode("");
            orderWarn.setId(IdUtils.fastSimpleUUID());
            orderWarn.setTitle(time+"监控信息清除定时任务");
            orderWarn.setCreateTime(new Date());
            orderWarn.setStatus("0");
            orderWarn.setType("监控任务");
            orderWarn.setContents(time+"监控信息清除定时任务执行：成功\n" +
                    "           清除日期："+time);
            orderWarn.setChangeNumber(i);
            orderWarnMapper.insertOrderWarn(orderWarn);
        }



    }
}
